package cn.explink.dop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.dop.service.OrderService;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 *
 * @author songkaojun
 */
@Controller
@RequestMapping("/orderMaintainController")
public class OrderMaintainController extends CgAutoListController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private DeliveryManService deliveryManService;

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/order/ordermaintain");
		return view;
	}

	@RequestMapping(params = "queryByCode")
	@ResponseBody
	public OrderVO queryByCode(String code, HttpServletRequest request) {
		try {
			OrderVO orderVO = new OrderVO();
			if (StringUtils.isBlank(code)) {
				return orderVO;
			} else {
				orderVO.setCode(code);
			}
			List<OrderVO> orderVOList = this.orderService.getListData(orderVO);
			if ((orderVOList != null) && (orderVOList.size() == 1)) {
				return orderVOList.get(0);
			} else {
				return new OrderVO();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderVO();
		}
	}

	@RequestMapping(params = "save")
	@ResponseBody
	public void save(OrderVO orderVO, HttpServletRequest request) {
		try {
			DeliveryManVO deliveryManVO=deliveryManService.queryById(orderVO.getDeliveryManID());
			OrderVO oriOrderVO=this.queryByCode(orderVO.getCode(), request);
			
			// 修改数据
			oriOrderVO.setDeliveryMan(deliveryManVO);
			oriOrderVO.setStatus(orderVO.getStatus());
			oriOrderVO.setDetail(orderVO.getDetail());
			oriOrderVO.setTotalPrice(orderVO.getTotalPrice());
			oriOrderVO.setCustName(orderVO.getCustName());
			oriOrderVO.setCustAddress(orderVO.getCustAddress());
			oriOrderVO.setTelephone(orderVO.getTelephone());
			oriOrderVO.setNote(orderVO.getNote());
			oriOrderVO.setDeliveryTime(orderVO.getDeliveryTime());
			oriOrderVO.setPayMethod(orderVO.getPayMethod());
			
			
			this.orderService.saveOrUpdate(oriOrderVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
